package cn.com.xinjinfu.pdoulcProduct.utils;

import android.app.Activity;
import android.app.Application;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;



import org.apache.http.params.HttpConnectionParams;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.text.DecimalFormat;

/**
 * Created by zhouwenbo on 15/6/12.
 * 通用工具类
 */
public class CommonUtils {
    /**just a tag*/
    public static final String TAG = "CommonUtils";

    /** 服务端返回的MD5加过盐，需要特殊处理 */
    public static final String MD5_SALT = "20150616duilu";
    /**1万*/
    public static final int UNIT_WAN  = 10000;

    /**
     * 根据手机分辨率将dp转化为px
     *
     * @param context context
     * @param dpValue dp值
     * @return dp值对应的px值
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * 根据手机的分辨率将px转化为dp
     *
     * @param context context
     * @param pxValue px值
     * @return px对应的dp值
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }


    /**
     * 获取状态栏高度
     *
     * @param activity context
     * @return 返回数值单位为px
     */
    public static int getTitleBarHeight(Activity activity) {
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }


    /**
     * 获取状态栏高度，此方法重载的意义是，上面的方法在Activity onCreate中返回始终为0
     * 而此方法则可以避免上述问题
     *
     * @param context context
     * @return 返回数值单位为px
     */
    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            if (c != null) {
                obj = c.newInstance();
                field = c.getField("status_bar_height");
                if (obj != null && field != null) {
                    Object o = field.get(obj);
                    if (o != null) {
                        int x = Integer.parseInt(o.toString());
                        return context.getResources().getDimensionPixelSize(x);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * toast提示
     *
     * @param ctx context
     * @param msg 信息
     */
    public static void toast(Context ctx, String msg) {
        Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
    }


    /**
     * toast提示
     *
     * @param ctx context
     * @param resId String 资源Id
     */
    public static void toast(Context ctx, int resId) {
        Toast.makeText(ctx, ctx.getResources().getString(resId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 获取sdcard下缓存文件夹
     *
     * @param context    ctx
     * @param uniqueName 文件夹的名字
     * @return 缓存文件目录
     */

    public File getDiskCacheDir(Context context, String uniqueName) {
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        return new File(cachePath + File.separator + uniqueName);
    }


    /**
     * 获取应用的VersionCode
     *
     * @param context context
     * @return versionCode
     */
    public static int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            versionCode = info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }








    /**
     * 获取网络是否可用
     *
     * @param context context
     * @return 网络是否可用
     */
    public static boolean isNetworkAvailable(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
        } catch (Exception e) {
            Log.v("connectivity", e.toString());
        }
        return false;
    }

    /**
     * 获取屏幕宽度
     *
     * @param context context
     * @return 屏幕宽度px值
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }


    /**
     * 获取屏幕高度
     *
     * @param context context
     * @return 屏幕高度px值
     */
    public static int getScreenHeight(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.heightPixels;
    }


}
